//
//  MusicTool.m
//  DayDayRun
//
//  Created by 卫建 on 16/1/11.
//  Copyright © 2016年 卫建. All rights reserved.
//

#import "MusicTool.h"

static NSMutableDictionary *_musicPlayers;

static NSMutableDictionary *_soundIDs;

@implementation MusicTool

#pragma mark-----存放所有的音乐播放器
+(NSMutableDictionary *)musicPlayers
{
    if (_musicPlayers == nil)
    {
        _musicPlayers = [NSMutableDictionary dictionary];
    }
    return _musicPlayers;
}

#pragma mark-----存放所有的音效ID
+(NSMutableDictionary *)soundIDs
{
    if (_soundIDs == nil)
    {
        _soundIDs = [NSMutableDictionary dictionary];
    }
    return _soundIDs;
}

#pragma mark----播放音乐文件
+(BOOL)playMusic:(NSString *)filename
{
    //如果没有传入文件名，直接返回
    if (!filename)
    {
        return NO;
    }
    
    //1.取出对应的播放器
    AVAudioPlayer *player = [self musicPlayers][filename];
    
    //如果播放器没有创建，那么就进行初始化
    if (!player)
    {
        //2.
        //音频文件的URL
        NSURL *url = [[NSBundle mainBundle]URLForResource:filename withExtension:nil];
        if (!url)
        {
            return NO;//如果URL为空，那么直接返回
        }
        //创建播放器
        player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
        
        //缓冲
        if (![player prepareToPlay])
        {
            return NO;
        }
        
        //存入字典
        [self musicPlayers][filename] = player;
    }
    
    //3.播放
    if (![player isPlaying])
    {
        //如果当前没处于播放状态，那么就播放
        return [player play];
    }
    
    return YES;//正在播放，返回yes
}

#pragma mark----暂停播放
+(void)pauseMusic:(NSString *)filename
{
    if (!filename)
    {
        return;
    }
    //1.取出对应的播放器
    AVAudioPlayer *player = [self musicPlayers][filename];
    
    //2.暂停
    [player pause];
}

#pragma mark----暂停音乐文件
+(void)stopMusic:(NSString *)filename
{
    if (!filename)
    {
        return;//如果没有传入文件名，那么就直接返回
    }
    //1.取出对应的播放器
    AVAudioPlayer *player = [self musicPlayers][filename];
    
    //2.停止
    [player stop];
    
    //3.将播放器从字典中移除
    [[self musicPlayers]removeObjectForKey:filename];
    
}

#pragma mark----播放音效文件
+(void)playSound:(NSString *)filename
{
    if (!filename)
    {
        return;
    }
    
    //1.取出对应的音效
    SystemSoundID soundID = (unsigned)[[self soundIDs][filename]unsignedIntegerValue];
    //2.播放音效
    //如果音效ID不存在,那么就创建
    if (!soundID)
    {
        //音效文件的URL
        NSURL *url = [[NSBundle mainBundle]URLForResource:filename withExtension:nil];
        if (!url)
        {
            return;//如果URL不存在，那么就直接返回
        }
        
        OSStatus status = AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);
        NSLog(@"%d",(int)status);
        
        //存入到字典中
        [self soundIDs][filename] = @(soundID);
    }
    
    //有音效后，播放音效
    AudioServicesPlaySystemSound(soundID);
}


#pragma mark----销毁音效
+(void)disposeSound:(NSString *)filename
{
    //如果传入的文件为空，那么就直接返回
    if (!filename)
    {
        return;
    }
    
    //1.取出对应的音效
    SystemSoundID soundID = (unsigned)[[self soundIDs][filename] unsignedIntegerValue];
    
    //2.销毁
    if (soundID)
    {
        AudioServicesDisposeSystemSoundID(soundID);
        
        //2.1销毁后，从字典中移除
        [[self soundIDs]removeObjectForKey:filename];
    }
    
}

@end
